/*
 * Copyright (c) 2012 Johan Fylling
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy 
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights 
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 
 * copies of the Software, and to permit persons to whom the Software is 
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in 
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 
 * THE SOFTWARE.
 */

library SideScrollingExample;
import "dart:html";
import "../example_lib.dart";
import "../../client/client_lib.dart";
import "../../engine/engine_lib.dart";
import "../../math/math_lib.dart";
import "../../tools/tools_lib.dart";


class SideScrollingExample implements Example {
  WebGameEngine       _engine;
  CanvasEngine        _graphicsEngine;
  PhysicsEngine       _physicsEngine;
  ParticleWorldState  _worldState;
  BroadphaseDetector  _broadphaseDetector;
  NarrowphaseDetector _narrowphaseDetector;
  ContactDetector     _contactDetector;
  CollisionManager    _collisionManager;
  
  SideScrollingExample () {
    InputManager inputManager = new InputManager(query("#canvas"));
    ServiceManager.setService("InputManager", inputManager);
    
    StaticIntegrator integrator = new StaticIntegrator(32);
    this._physicsEngine = new ParticleEngine(integrator, 32, 1, new Vector2(0.0, 9.8));
    this._graphicsEngine = new CanvasEngine("canvas", this._broadphaseDetector);
    ServiceManager.setService("DebugPainter", this._graphicsEngine);
    this._worldState = new ParticleWorldState();
    this._broadphaseDetector = new SapDetector();
    this._graphicsEngine.bpd = this._broadphaseDetector;
    this._narrowphaseDetector = new SatDetector();
    this._contactDetector = new ClippingContactDetector();
    this._collisionManager = new CollisionManager(this._broadphaseDetector,
                                                  this._narrowphaseDetector,
                                                  this._contactDetector);
    this._engine = new WebGameEngine(this._physicsEngine, 
                                     this._graphicsEngine, 
                                     this._worldState,
                                     this._collisionManager);
  }
  
  void init () {
    WorldObject obj = new WorldObject();
    obj.body.collidable = new CollidableRectangle(new Vector2(200, 10));
    obj.body.position = new Vector2(0,100);
    this._broadphaseDetector.addDynamicBody(obj.body);
    this._worldState.addWorldObject(obj);
    
    obj = new WorldObject();
    obj.body.collidable = new CollidableRectangle(new Vector2(10, 90));
    obj.body.position = new Vector2(-210,0);
    this._broadphaseDetector.addDynamicBody(obj.body);
    this._worldState.addWorldObject(obj);
    
    obj = new WorldObject();
    obj.body.collidable = new CollidableRectangle(new Vector2(10, 90));
    obj.body.position = new Vector2(210,0);
    this._broadphaseDetector.addDynamicBody(obj.body);
    this._worldState.addWorldObject(obj);
    
    
    obj = new WorldObject(new ParticleSystem.fromCollidable(new CollidableCircle(20), 100));
    obj.body.position = new Vector2(50,30);
    this._broadphaseDetector.addDynamicBody(obj.body);
    this._worldState.addWorldObject(obj);
    
    for (int i = 0; i < 10; i++) {
      obj = new WorldObject(new ParticleSystem.fromCollidable(new CollidableRectangle(new Vector2(10, 20)), 10));
      obj.body.position = new Vector2(-100 + 30*i, -200);
      this._broadphaseDetector.addDynamicBody(obj.body);
      this._worldState.addWorldObject(obj);
    }
    
    obj = new WorldObject();
    obj.body.collidable = new CollidableCircle(20);
    this._broadphaseDetector.addDynamicBody(obj.body);
    this._worldState.addWorldObject(obj);
    var element = query("#canvas");
    element.onMouseMove.listen((MouseEvent event) {
      obj.body.position = _graphicsEngine.camera.canvas2world(new Vector2(event.offsetX, event.offsetY));
    });
  }
  
  void start () { this._engine.start(); }
  
  void stop () {}
}

void main() {
  SideScrollingExample example = new SideScrollingExample();
  example.init();
  example.start();
}
